package com.cloud.school.bus.teacherhelper.modules.home.adapter;

import java.io.File;
import java.util.List;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import com.android.support.jhf.debug.DebugLog;
import com.android.support.jhf.universalimageloader.core.ImageLoader;
import com.android.support.jhf.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.android.support.jhf.utils.ImageUtil;
import com.android.support.jhf.utils.ScreenUtils;
import com.android.support.jhf.utils.ToolUtils;
import com.cloud.school.bus.teacherhelper.R;
import com.cloud.school.bus.teacherhelper.TeacherHelperApplication;
import com.cloud.school.bus.teacherhelper.entitys.Picture;
import com.cloud.school.bus.teacherhelper.entitys.Student;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class PictureSelectedAdapter extends BaseAdapter {

	private Context mContext;
	private TeacherHelperApplication mApplication;
	private List<Picture> mPictures;

	private int mBounds = 0;

	public PictureSelectedAdapter(Context context, List<Picture> pictures) {
		mContext = context;
		mPictures = pictures;
		mApplication = (TeacherHelperApplication) mContext
				.getApplicationContext();
		int[] bounds = ScreenUtils.getScreenBounds(mContext);
		mBounds = (bounds[0] - ToolUtils.dipToPx(mContext, 10)) / 4;
	}

	@Override
	public int getCount() {
		return mPictures.size();
	}

	@Override
	public Object getItem(int arg0) {
		return mPictures.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {

		if (null == arg1) {
			arg1 = LayoutInflater.from(mContext).inflate(
					R.layout.adapter_picture_selected, null);
		}

		final Picture picture = mPictures.get(arg0);

		final ImageView selectImageView = (ImageView) arg1
				.findViewById(R.id.selectImageView);
		if (picture.isSelected) {
			selectImageView.setVisibility(View.VISIBLE);
		} else {
			selectImageView.setVisibility(View.GONE);
		}
		ImageView headImageView = (ImageView) arg1
				.findViewById(R.id.headImageView);
		headImageView.setLayoutParams(new RelativeLayout.LayoutParams(mBounds,
				mBounds));
		
		if(TextUtils.isEmpty(picture.getThumbPath())){
			//没有缩略图
			
			if (!TextUtils.isEmpty(picture.getPicturePath())) {
				DebugLog.logI(picture.getPicturePath());
				ImageLoader.getInstance().displayImage(picture.getPicturePath(),
						headImageView, mApplication.mNoCacheDisplayImageOptions,
						new SimpleImageLoadingListener() {
							@Override
							public void onLoadingComplete(String imageUri,
									View view, Bitmap loadedImage) {
								ImageView imageView = (ImageView) view;
								if(imageUri.startsWith("http://")){
									imageView.setImageBitmap(loadedImage);
								}else if(imageUri.startsWith("file:///")){
									ImageUtil.setRotaingImageBitmap(imageUri.replace("file:///", "/"), loadedImage, imageView);
								}else {
									ImageUtil.setRotaingImageBitmap(imageUri, loadedImage, imageView);
								}
							}
						});
			}
			
		}else {
			//有缩略图
			ImageLoader.getInstance().displayImage(picture.getThumbPath(),
					headImageView, mApplication.mNoCacheDisplayImageOptions,
					new SimpleImageLoadingListener() {
						@Override
						public void onLoadingComplete(String imageUri,
								View view, Bitmap loadedImage) {
							ImageView imageView = (ImageView) view;
							if(imageUri.startsWith("http://")){
								imageView.setImageBitmap(loadedImage);
							}else if(imageUri.startsWith("file:///")){
								ImageUtil.setRotaingImageBitmap(imageUri.replace("file:///", "/"), loadedImage, imageView);
							}else {
								ImageUtil.setRotaingImageBitmap(imageUri, loadedImage, imageView);
							}
						}
					});
		}
		
		
		ViewGroup itemLayout = (ViewGroup) arg1.findViewById(R.id.itemLayout);
		itemLayout.setLayoutParams(new LinearLayout.LayoutParams(mBounds,
				mBounds));

		return arg1;
	}

	public void clearAllSelected(){
		for (int i = 0; i < mPictures.size(); i++) {
			mPictures.get(i).isSelected = false;
		}
	}
	
}
